using System;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections.Generic;
using WebSite.Admin.Core;

namespace WebSite.Admin
{
	public partial class Admin_ShortMessage : AdminBasePage
	{
	
	// Coded by wenbin 2012-9-25 0:01:22
		#region 实体类
		/// <summary>
		/// 实体类
		/// </summary>
		private class ShortMessage
		{
			private int _id = 0;	
			public  int  Id  
			{
				get{ return  _id; }
				set{ _id = value; }
			}
			private int _recevieBabyId = 0;	
			public  int  RecevieBabyId  
			{
				get{ return  _recevieBabyId; }
				set{ _recevieBabyId = value; }
			}
			private int _sendBabyId = 0;	
			public  int  SendBabyId  
			{
				get{ return  _sendBabyId; }
				set{ _sendBabyId = value; }
			}
			private string _sendBabyName = string.Empty;	
			public  string  SendBabyName  
			{
				get{ return  _sendBabyName; }
				set{ _sendBabyName = value; }
			}
			private string _sendBabyHeadImg = string.Empty;	
			public  string  SendBabyHeadImg  
			{
				get{ return  _sendBabyHeadImg; }
				set{ _sendBabyHeadImg = value; }
			}
			private int _msgType = 0;	
			public  int  MsgType  
			{
				get{ return  _msgType; }
				set{ _msgType = value; }
			}
			private string _msgContent = string.Empty;	
			public  string  MsgContent  
			{
				get{ return  _msgContent; }
				set{ _msgContent = value; }
			}
			private int _isRead = 0;	
			public  int  IsRead  
			{
				get{ return  _isRead; }
				set{ _isRead = value; }
			}
			private int _isDelete = 0;	
			public  int  IsDelete  
			{
				get{ return  _isDelete; }
				set{ _isDelete = value; }
			}
			private DateTime _createTime = DateTime.Now;	
			public  DateTime  CreateTime  
			{
				get{ return  _createTime; }
				set{ _createTime = value; }
			}
			private string _ip = string.Empty;	
			public  string  Ip  
			{
				get{ return  _ip; }
				set{ _ip = value; }
			}
	
	}
		#endregion
	
		dbHelper db = new dbHelper();
		
		/// <summary>
        /// 查询id
        /// </summary>
        public long QId
        {
            get
            {
                return Convert.ToInt64(Request.QueryString["id"]);
            }
        }
		
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
		}
	
	
		protected void Page_Load(object sender, EventArgs e)
		{
			string redirectUrl = HttpContext.Current.Request.Url.ToString().Split('?')[0];
			int page = Convert.ToInt32(Request.QueryString["page"]);
			if (page == 0)
				page = 1;
			string urlPage = "page=" + page;
			string strWhere = " 1=1 ";
			string urlParams = "";
			string searchId= dbHelper.FilterSql(Request.QueryString["searchId"]);
			if (!string.IsNullOrEmpty(searchId))
			{
				strWhere +=" and Id ="+searchId+"";
				urlParams += "&searchId=" + searchId;
			}
			string searchRecevieBabyId= dbHelper.FilterSql(Request.QueryString["searchRecevieBabyId"]);
			if (!string.IsNullOrEmpty(searchRecevieBabyId))
			{
				strWhere +=" and RecevieBabyId ="+searchRecevieBabyId+"";
				urlParams += "&searchRecevieBabyId=" + searchRecevieBabyId;
			}
			string searchSendBabyId= dbHelper.FilterSql(Request.QueryString["searchSendBabyId"]);
			if (!string.IsNullOrEmpty(searchSendBabyId))
			{
				strWhere +=" and SendBabyId ="+searchSendBabyId+"";
				urlParams += "&searchSendBabyId=" + searchSendBabyId;
			}
			string searchSendBabyName= dbHelper.FilterSql(Request.QueryString["searchSendBabyName"]);
			if (!string.IsNullOrEmpty(searchSendBabyName))
			{
				strWhere +=" and SendBabyName like '%"+searchSendBabyName+"%'";
				urlParams += "&searchSendBabyName=" + searchSendBabyName;
			}
			string searchSendBabyHeadImg= dbHelper.FilterSql(Request.QueryString["searchSendBabyHeadImg"]);
			if (!string.IsNullOrEmpty(searchSendBabyHeadImg))
			{
				strWhere +=" and SendBabyHeadImg like '%"+searchSendBabyHeadImg+"%'";
				urlParams += "&searchSendBabyHeadImg=" + searchSendBabyHeadImg;
			}
			string searchMsgType= dbHelper.FilterSql(Request.QueryString["searchMsgType"]);
			if (!string.IsNullOrEmpty(searchMsgType))
			{
				strWhere +=" and MsgType ="+searchMsgType+"";
				urlParams += "&searchMsgType=" + searchMsgType;
			}
			string searchMsgContent= dbHelper.FilterSql(Request.QueryString["searchMsgContent"]);
			if (!string.IsNullOrEmpty(searchMsgContent))
			{
				strWhere +=" and MsgContent like '%"+searchMsgContent+"%'";
				urlParams += "&searchMsgContent=" + searchMsgContent;
			}
			string searchIsRead= dbHelper.FilterSql(Request.QueryString["searchIsRead"]);
			if (!string.IsNullOrEmpty(searchIsRead))
			{
				strWhere +=" and IsRead ="+searchIsRead+"";
				urlParams += "&searchIsRead=" + searchIsRead;
			}
			string searchIsDelete= dbHelper.FilterSql(Request.QueryString["searchIsDelete"]);
			if (!string.IsNullOrEmpty(searchIsDelete))
			{
				strWhere +=" and IsDelete ="+searchIsDelete+"";
				urlParams += "&searchIsDelete=" + searchIsDelete;
			}
			string searchCreateTime= dbHelper.FilterSql(Request.QueryString["searchCreateTime"]);
			if (!string.IsNullOrEmpty(searchCreateTime))
			{
				strWhere +=" and CreateTime like '%"+searchCreateTime+"%'";
				urlParams += "&searchCreateTime=" + searchCreateTime;
			}
			string searchIp= dbHelper.FilterSql(Request.QueryString["searchIp"]);
			if (!string.IsNullOrEmpty(searchIp))
			{
				strWhere +=" and Ip like '%"+searchIp+"%'";
				urlParams += "&searchIp=" + searchIp;
			}
			
			urlPage = urlPage + urlParams;
			if(!string.IsNullOrEmpty(urlParams))
				urlParams = urlParams.Substring(1, urlParams.Length - 1);
			redirectUrl = redirectUrl + "?" + urlPage;
			
			//数据导出
	 		string toExcel = dbHelper.FilterSql(Request.QueryString["toExcel"]);
            if (toExcel == "1")
            {
                ExcelSubmit(strWhere);
            }
	
			//分页
			Paging(page, strWhere, urlParams);
	
			//业务
			BussessOper(redirectUrl);
	
		
		}
	
	  	#region  --数据导出---
       
        /// <summary>
        /// 保存xls文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ExcelSubmit(string sWhere)
        {		 	
			string sSelect = "Id as 'Id',RecevieBabyId as '接受方宝宝Id',SendBabyId as '发送方宝宝Id',SendBabyName as '发送方宝宝名称',SendBabyHeadImg as '发送方宝宝头像',MsgType as '消息类别',MsgContent as '消息内容',IsRead as '是否已读',IsDelete as '是否删除',CreateTime as '时间',Ip as 'Ip'";
			
            string some_search = string.Format("select {0} from ShortMessage(nolock) where {1} ", sSelect, sWhere); //查询语句
            DataSet ds = db.returndataset(null, some_search, null, null);

            //填冲到XLS中////
            AdminCommon.xlsGridview(ds, string.Format("数据_{0}", DateTime.Now));
        }

        #endregion
	
	
		#region 分页
		public void Paging(int page, string strWhere, string urlParams)
		{
			dbHelper.pageproret record = new dbHelper.pageproret();
	
			record = db.GetPageRecordSet("ShortMessage", page, 20, "Id", "Id Desc", "*", strWhere);
			int pagecount = record.pagecount;
			DataTable dt = record.recordset;
			List<ShortMessage> datasrc = db.ConvertTo<ShortMessage>(dt).ToList();
				
			 DataList.DataSource = datasrc.Count > 0 ? datasrc : null;
            DataList.DataBind();
		
			string pager = db.BuildPager(page, pagecount, "", urlParams);
			sPage.Text = pager;
		}
	
		#endregion
	
		#region 业务处理
		/// <summary>
		/// 业务处理
		/// </summary>
		/// <param name="redirectUrl"></param>
		public void BussessOper(string redirectUrl)
		{
			if (Request.QueryString["act"] == "del")
			{				
				if (QId != 0)
				{
					DeleteData(QId);
					Response.Redirect(redirectUrl);
				}
			}
	
			if (Request.ServerVariables["REQUEST_METHOD"] == "POST")
			{
				string idStr = dbHelper.FilterSql(Request.Form["SelectID"]);
				if (!string.IsNullOrEmpty(idStr))
				{
					idStr = idStr.Replace(" ", "");
					idStr = idStr.Replace("&nbsp;", "");
					if (!string.IsNullOrEmpty(Request.Form["delete"]))
					{
						DeleteBatchData(idStr);
						Response.Redirect(redirectUrl);
					}
					if (!string.IsNullOrEmpty(Request.Form["pingbi"]))
					{
						SwitchBatchData(0, idStr);
						Response.Redirect(redirectUrl);
					}
					if (!string.IsNullOrEmpty(Request.Form["fabu"]))
					{
						SwitchBatchData(1, idStr);
						Response.Redirect(redirectUrl);
					}
					Response.Redirect(redirectUrl);
					Response.End();
				}
				ShortMessage sdata = new ShortMessage();
				
				//string modid = Request.Form["ModId"] == null ? "0" : Request.Form["ModId"].Trim();
				//int id =sdata.Id = Convert.ToInt32(modid);
					sdata.Id  = Convert.ToInt32(dbHelper.FilterSql(Request.Form["ModId"].Trim())?? "");
					sdata.RecevieBabyId  = Convert.ToInt32(dbHelper.FilterSql(Request.Form["ModRecevieBabyId"].Trim())?? "");
					sdata.SendBabyId  = Convert.ToInt32(dbHelper.FilterSql(Request.Form["ModSendBabyId"].Trim())?? "");
					sdata.SendBabyName  = Convert.ToString(dbHelper.FilterSql(Request.Form["ModSendBabyName"].Trim())?? "");
					sdata.SendBabyHeadImg  = Convert.ToString(dbHelper.FilterSql(Request.Form["ModSendBabyHeadImg"].Trim())?? "");
					sdata.MsgType  = Convert.ToInt32(dbHelper.FilterSql(Request.Form["ModMsgType"].Trim())?? "");
					sdata.MsgContent  = Convert.ToString(dbHelper.FilterSql(Request.Form["ModMsgContent"].Trim())?? "");
					sdata.IsRead  = Convert.ToInt32(dbHelper.FilterSql(Request.Form["ModIsRead"].Trim())?? "");
					sdata.IsDelete  = Convert.ToInt32(dbHelper.FilterSql(Request.Form["ModIsDelete"].Trim())?? "");
					sdata.CreateTime  = Convert.ToDateTime((Request.Form["ModCreateTime"].Trim())?? "");
					sdata.Ip  = Convert.ToString(dbHelper.FilterSql(Request.Form["ModIp"].Trim())?? "");
				var id = sdata.Id;
				if (id == 0)
				{
					//增加
					if (InsertData(sdata))
					{
						Response.Redirect(redirectUrl);
						Response.End();
					}
					else
					{
						Response.Write("error");
						Response.End();
					}
				}
				else
				{
					//修改
					if (UpdateData(sdata))
					{
						Response.Redirect(redirectUrl);
						Response.End();
					}
					else
					{
						Response.Write("error");
						Response.End();
					}
				}
			}
		}
		#endregion
		
		
		#region 增删改
		
		/// <summary>
		/// 添加数据
		/// </summary>
		/// <param name="sdata"></param>
		/// <returns></returns>
		private bool InsertData(ShortMessage sdata)
		{
			string sqlStr = @"INSERT INTO ShortMessage(
					[RecevieBabyId],
							[SendBabyId],
							[SendBabyName],
							[SendBabyHeadImg],
							[MsgType],
							[MsgContent],
							[IsRead],
							[IsDelete],
							[CreateTime],
							[Ip]	)VALUES(
					@RecevieBabyId,
							@SendBabyId,
							@SendBabyName,
							@SendBabyHeadImg,
							@MsgType,
							@MsgContent,
							@IsRead,
							@IsDelete,
							@CreateTime,
							@Ip)";
			SqlParameter[] parms = new SqlParameter[] {
					new SqlParameter("@RecevieBabyId",sdata.RecevieBabyId),
							new SqlParameter("@SendBabyId",sdata.SendBabyId),
							new SqlParameter("@SendBabyName",sdata.SendBabyName),
							new SqlParameter("@SendBabyHeadImg",sdata.SendBabyHeadImg),
							new SqlParameter("@MsgType",sdata.MsgType),
							new SqlParameter("@MsgContent",sdata.MsgContent),
							new SqlParameter("@IsRead",sdata.IsRead),
							new SqlParameter("@IsDelete",sdata.IsDelete),
							new SqlParameter("@CreateTime",sdata.CreateTime),
							new SqlParameter("@Ip",sdata.Ip)};
			int ret = db.GetExecuteNonQuery(null, sqlStr, parms);
			return ret > 0;
		}
		
		
		/// <summary>
		/// 修改数据
		/// </summary>		
		/// <param name="sdata"></param>
		/// <returns></returns>
		private bool UpdateData(ShortMessage sdata)
		{
			string sqlStr = @"UPDATE ShortMessage SET
					[RecevieBabyId]=@RecevieBabyId,
										[SendBabyId]=@SendBabyId,
										[SendBabyName]=@SendBabyName,
										[SendBabyHeadImg]=@SendBabyHeadImg,
										[MsgType]=@MsgType,
										[MsgContent]=@MsgContent,
										[IsRead]=@IsRead,
										[IsDelete]=@IsDelete,
										[CreateTime]=@CreateTime,
										[Ip]=@Ip WHERE Id=@Id";
				
				SqlParameter[] parms = new SqlParameter[] {
					new SqlParameter("@Id",sdata.Id),
										new SqlParameter("@RecevieBabyId",sdata.RecevieBabyId),
										new SqlParameter("@SendBabyId",sdata.SendBabyId),
										new SqlParameter("@SendBabyName",sdata.SendBabyName),
										new SqlParameter("@SendBabyHeadImg",sdata.SendBabyHeadImg),
										new SqlParameter("@MsgType",sdata.MsgType),
										new SqlParameter("@MsgContent",sdata.MsgContent),
										new SqlParameter("@IsRead",sdata.IsRead),
										new SqlParameter("@IsDelete",sdata.IsDelete),
										new SqlParameter("@CreateTime",sdata.CreateTime),
										new SqlParameter("@Ip",sdata.Ip)};
			int ret = db.GetExecuteNonQuery(null, sqlStr, parms);
			return ret > 0;
		}
		
		
		/// <summary>
		///单个删除
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		private bool DeleteData(long id)
		{
			string sqlStr = string.Format("DELETE FROM ShortMessage WHERE Id={0}",id);
			
			int ret = db.GetExecuteNonQuery(null, sqlStr, null);
			return ret > 0;
		}
		/// <summary>
		/// 批量删除
		/// </summary>
		/// <param name="idStr"></param>
		/// <returns></returns>
		private bool DeleteBatchData(string idStr)
		{
			string sqlStr = string.Format("DELETE FROM ShortMessage WHERE Id IN ({0})",idStr);
	
			int ret = db.GetExecuteNonQuery(null, sqlStr, null);
			return ret > 0;
		}
		
		
		/// <summary>
		/// 状态操作
		/// </summary>
		/// <param name="optype"></param>
		/// <param name="idStr"></param>
		/// <returns></returns>
		private bool SwitchBatchData(int optype,string idStr)
		{
			string sqlStr = "";
			if(optype==0)
				sqlStr = string.Format("UPDATE ShortMessage SET [Status]=0 WHERE Id IN ({0})",idStr);
			else
				sqlStr = string.Format("UPDATE ShortMessage SET [Status]=1 WHERE Id IN ({0})", idStr);
			
			int ret = db.GetExecuteNonQuery(null, sqlStr, null);
			return ret > 0;
		}
		

		
		#endregion
	}
}


